# -*- coding: utf-8 -*-

'''eof
name:战略新兴产业
code:Industry_Score_28
tableName:
columnName:
groups:行业评分模块
dependencies:INDUSTRY_INFO
type:常用指标
datasourceType:在线指标
description:
eof'''

from dateutil.parser import parse
import datetime
import pandas as pd

null_type_list = ['', None, 'None', 'null', 'Null', 'NULL', '/', ' ',[]]

def timeCompare2(a,time):
    List = list(a)
    error = 0
    result = []
    for i in List:
        try:
            if parse(i).date() == time:
                result.append(True)
            else:
                result.append(False)
        except:
            error += 1
            result.append(False)
    if len(List) == error:
        raise RuntimeError(u"日期字段解析全部失败") 
    return result


def Industry_Score_28():
    data = INDUSTRY_INFO.get("data")
    if data in null_type_list:
        return u"缺失值"
    else:
        #计算factor1
        industryTag = data.get("industryTag")
        if industryTag in null_type_list:
            return u"缺失值"
        else:
            DATA = pd.DataFrame(industryTag)
            if len(DATA) == []:
                return u"缺失值" 
            timeList = []
            timeError = 0
            for i in DATA["statisticsDate"]:
                try:
                    timeList.append(parse(i).date())
                except:
                    timeError += 1
            if timeError == len(DATA):
                raise RuntimeError(u"统计日期字段解析全部失败") 
            timeMax = max(timeList)
            DATA = DATA[pd.Series(timeCompare2(DATA["statisticsDate"],timeMax),index=DATA.index)].reset_index(drop=True)
            if u"战略新兴产业" in list(DATA["topicTag"]):
                return 1
            else:
                return 0
        
result = Industry_Score_28()